package com.lin.servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.lin.service.IUserService;
import com.lin.service.UserServiceImpl;
import com.lin.utils.BaseServlet;
import com.lin.vo.User;
/**
 * 前台：用户(控制器)
 * @author manage
 *
 */
@WebServlet(name="UserServlet",urlPatterns={"/userServlet"})
public class UserServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private IUserService userService=new UserServiceImpl();
	/**
	 * 用户注册方法
	 * @param request
	 * @param response
	 * @return
	 */
	public String regist(HttpServletRequest request,HttpServletResponse response){
		try {
			/**
			 * 1.封装表单数据信息
			 */
			User user=new User();
			BeanUtils.populate(user, request.getParameterMap());
			/**
			 * 2.调用业务层方法
			 */
			 userService.regist(user);
		    /**
			 * 3.页面跳转
			 */
			 request.setAttribute("msg", "用户注册成功，请去邮箱激活");
		} catch (IllegalAccessException | InvocationTargetException e) {
			e.printStackTrace();
		}
		return "change:/jsps/msg.jsp";
	}
	
	/**
	 * 用户激活邮件方法
	 * @param request
	 * @param response
	 * @return
	 */
	public String active(HttpServletRequest request,HttpServletResponse response){
		//1.获取激活码
		String code = request.getParameter("code");
		System.out.println(code);
		//2.调用业务层,通过激活码查找用户
		User user=userService.findByCodeUser(code);
		if(user!=null){
			//表示已经激活成功,需要将用户的状态修改成  1,并且将激活码设置为null(防止重复提交)
			user.setState(1);
			user.setCode(null);
			//3.业务层调用修改激活码的方法
			userService.update(user);
			//4.保存激活成功信息
			request.setAttribute("msg", "激活成功，可以登录！");
		}else{
			request.setAttribute("msg", "激活已经失效!");
		}
		return "change:/jsps/msg.jsp";
	}
	
	public String checkUserName(HttpServletRequest request,HttpServletResponse response) throws IOException{
		response.setContentType("text/json;charset=UTF-8");
		//1.获取用户数据
		String userName=request.getParameter("username");
		System.out.println(userName);
		boolean flag=userService.validateAjaxUserName(userName);
		System.out.println(flag);
		response.getWriter().println(flag);
		return "";
	}
	public String login(HttpServletRequest request,HttpServletResponse response){
		//1.封装表单数据
		User user=new User();
		try {
			BeanUtils.populate(user, request.getParameterMap());
			User exitUser=userService.login(user);
			System.out.println(exitUser);
			if(exitUser!=null && 1==exitUser.getState()){
				request.getSession().setAttribute("exitUser", exitUser);
				return "orient:/jsps/main.jsp";
			}else{
				request.setAttribute("msg", "用户密码错误或没激活");
				return "change:/jsps/user/login.jsp";
			}
		} catch (IllegalAccessException | InvocationTargetException e) {
			e.printStackTrace();
		}
		return "";
	}
	public String loginQuit(HttpServletRequest request,HttpServletResponse response){
		 request.getSession().invalidate();
		 return "orient:/jsps/top.jsp";
	}
	public String ajaxCodeImg(HttpServletRequest request,HttpServletResponse response) throws IOException{
		//1.获取验证码参数
		String imgCode=(String) request.getSession().getAttribute("imgCode");
		//2.获取AJAX参数值
		String captcha=request.getParameter("captcha");
		if(captcha.equalsIgnoreCase(imgCode)){
			response.getWriter().println("1");
		}else{
			response.getWriter().println("0");
		}
		return "";
	}
}
